package info_ctl

import (
	"net/http"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/info_mod"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/service/info_serv"
	"ubt-rbac-service/internal/app/service/sys_serv"
	"ubt-rbac-service/internal/app/util"
	"ubt-rbac-service/internal/core/convert"
	"ubt-rbac-service/internal/core/parse"
)

type infoCtl struct{}

var InfoCtl infoCtl

// Save 保存资讯
func (c *infoCtl) Save(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &info_mod.InfoSaveReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*info_mod.InfoSaveReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.InfoId = util.InputFilter(req.InfoId, true)
	req.FstTitle = util.InputFilter(req.FstTitle, false)
	req.SecTitle = util.InputFilter(req.SecTitle, false)
	req.InfoType = util.InputFilter(req.InfoType, true)
	req.Src = util.InputFilter(req.Src, true)
	req.Describ = util.TextFilter(req.Describ)
	req.FstImgId = util.InputFilter(req.FstImgId, true)
	req.FstImgName = util.InputFilter(req.FstImgName, true)
	req.FstImgUrl = util.InputFilter(req.FstImgUrl, true)
	req.ImgIds = util.InputFilter(req.ImgIds, true)
	req.ImgNames = util.InputFilter(req.ImgNames, true)
	req.ImgUrls = util.InputFilter(req.ImgUrls, true)
	req.FileIds = util.InputFilter(req.FileIds, true)
	req.FileNames = util.InputFilter(req.FileNames, true)
	req.FileUrls = util.InputFilter(req.FileUrls, true)
	req.TagIds = util.InputFilter(req.TagIds, true)
	req.TagNames = util.InputFilter(req.TagNames, true)
	// 判断编码是否重复
	extVo := &info_mod.InfoExtVo{InfoId: req.InfoId, FstTitle: req.FstTitle, InfoType: req.InfoType, OptOrg: userInfo.DeptId}
	if ext := info_serv.InfoServ.Exists(extVo); ext {
		util.ToJson(w, &model.ResJson{Code: 402, Msg: "对不起，填写的编码已存在，请更换！"})
		return
	}
	req.OptOrg = userInfo.DeptId
	var (
		result *model.Result
		kind   uint8
	)
	if req.InfoId == "" {
		// 新增
		result = info_serv.InfoServ.Add(req)
		kind = 1
	} else {
		// 修改
		result = info_serv.InfoServ.Update(req)
		kind = 3
	}
	if result != nil && result.RetCode == 1001 {
		var code string
		switch req.Kind {
		case 1:
			code = "10020103"
		case 2:
			code = "10020203"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: kind}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// Process 删除资讯
func (c *infoCtl) Process(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &info_mod.InfoProReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*info_mod.InfoProReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.InfoIds = util.InputFilter(req.InfoIds, true)
	result := info_serv.InfoServ.Process(req)
	if result != nil && result.RetCode == 1001 {
		var kind uint8
		switch req.States {
		case 1:
			kind = 4
		case 3:
			kind = 5
		case 2:
			kind = 2
		default:
			kind = 2
		}
		var code string
		switch req.Kind {
		case 1:
			code = "10020103"
		case 2:
			code = "10020203"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: kind}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// One 获取资讯详情
func (c *infoCtl) One(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	// 过滤关键参数值中的非法字符
	infoId := u.Get("infoId")
	infoId = util.InputFilter(infoId, true)
	if infoId == "" || len(infoId) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的编号参数为空或不正确，请重试！"})
		return
	}
	result := info_serv.InfoServ.One(infoId)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 404, Msg: result.RetMsg})
	}
}

// Page 获取资讯分页列表
func (c *infoCtl) Page(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	kinds := u.Get("kind")
	schName := u.Get("schName")
	infoType := u.Get("infoType")
	statess := u.Get("states")
	begins := u.Get("beginTime")
	ends := u.Get("endTime")
	limits := u.Get("limit")
	offsets := u.Get("offset")
	kinds = util.InputFilter(kinds, true)
	schName = util.InputFilter(schName, false)
	infoType = util.InputFilter(infoType, true)
	statess = util.InputFilter(statess, true)
	begins = util.InputFilter(begins, true)
	ends = util.InputFilter(ends, true)
	limits = util.InputFilter(limits, true)
	offsets = util.InputFilter(offsets, true)
	kind := convert.Str2u8(kinds)
	states := convert.Str2u8(statess)
	beginDate := convert.Str2int(begins)
	endDate := convert.Str2int(ends)
	limit := convert.Str2u8(limits)
	offset := convert.Str2u32(offsets)
	// 先统计信息的数量
	req := &info_mod.InfoPageReq{SchName: schName, InfoType: infoType, Kind: kind, States: states, BeginDate: beginDate, EndDate: endDate, OptOrg: userInfo.DeptId}
	if result := info_serv.InfoServ.Count(req); result != nil && result.RetCode == 1001 && result.RetData.(int64) > 0 {
		res := &info_mod.InfoPageRes{Total: result.RetData.(int64)}
		// 再查询具体的信息列表
		req.Limit = limit
		req.Offset = offset
		if result = info_serv.InfoServ.Page(req); result != nil && result.RetCode == 1001 {
			res.List = result.RetData
			util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: res})
			return
		}
	}
	util.ToJson(w, &model.ResJson{Code: 404, Msg: "暂无内容"})
}

// Sort 获取资讯数量，用于排序
func (c *infoCtl) Sort(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	infoType := u.Get("infoType")
	infoType = util.InputFilter(infoType, true)
	if infoType == "" || len(infoType) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数为空或不正确，请重试！"})
		return
	}
	result := info_serv.InfoServ.Sort(infoType)
	util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
}
